#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h> 
#include <string.h>
#include <stdlib.h>
#include <pthread.h>
#include <unistd.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/mman.h>
#include <errno.h>

//------------------------------------------------------

#include "buffermap.h"
#include "partition.h"
#include "../../structure.h"
#include "../../config.h"
#define QUERY_SIZE 4+1+32+1+1+MAX_PIECES*(1+PIECE_SIZE)+2  //rigoureusement ca serait log(nbre_pieces) du fichier à la place de MAX_PIECES
#define SHARED_REP "/net/i/aslimanihouti/S8/reseaux/network-project/Peer/Download"//./Database"
//------------------------------------------------------

//-------------------- à deplacer ----------------------
struct database db; 
typedef long long _t_buffermap ;



char * data_query(char * query, int gp_file, _t_buffermap bm){

    //buffermap bmp = create_buffermap(bm, db.tab_file[gp_file].size);
    //int size_bm = ceil(f->size*1.0/ PIECE_SIZE); 
    
    struct file * f = &db.tab_file[gp_file];
    size_t nbpieces = (f->size / f->piece_size) + ((f->size%(f->piece_size) != 0)?1:0);
    
    int bytes_query = sprintf(query, "data %.32s [", f->key);
    int j;
    char buffer[PIECE_SIZE];
    char file_path[QUERY_SIZE] = SHARED_REP;
    size_t bytes_read;
    
    strcat(file_path, f->name);
        
    for (j=0; j<nbpieces; j++){
        if (BM_isSet(j,bm)){
            
            bzero(buffer,PIECE_SIZE);
            bytes_read = split_file(file_path, j, buffer);

            printf("bytes_read %d\n", bytes_read);
            if (!bytes_read){
                fprintf(stderr, "ERROR : Reading piece %d aborted\n", j);
                return NULL;
            }

            bytes_query += sprintf(query+bytes_query, "%d:", j);

            memcpy(query+bytes_query, buffer, bytes_read);
            bytes_query += PIECE_SIZE; 

            bytes_query += sprintf(query+bytes_query, " ");
        }
    }
    sprintf(query+bytes_query-1, "]");
    return query;
}
  		
#ifdef TEST
void recompose(char * written_file_name, char * query, int gp_file, _t_buffermap bm){
    buffermap bmp = create_buffermap(bm, db.tab_file[gp_file].size);
    //fprintf(stderr, "COUCOU\n");
    print_buffermap(bmp);
    int j;
    char * buffer = malloc(QUERY_SIZE*sizeof(char));//PIECE_SIZE*sizeof(char));
    
    for (j=0; j<bmp.size; j++){
        if (bmp.buffer[j] == '1'){
           
        	strcpy(buffer, query);
            buffer = strchr(buffer, ':')+1;
            recompose_file(written_file_name, j, buffer, PIECE_SIZE);//PIECE_SIZE à modifier par bytes_read !!(car de la derniere piece)
            //query += 2048;
  	    }
    }
    //free(buffer);
}

  	
int main(){
	char query[QUERY_SIZE];
	int gp_file = 0;

	// initialisation de la base de données
	db.nb_file = 1;
	strcpy(db.tab_file[gp_file].name, "images.jpg")	;
	db.tab_file[gp_file].size = 8430; 
	db.tab_file[gp_file].piece_size = 2048;
	strcpy(db.tab_file[gp_file].key, "b19c5ed1aee852433a310ba995422d00");

	//db.tab_file[gp_file].buffer_map = 145;//10010001
	db.tab_file[gp_file].clients = NULL; 
	_t_buffermap bm = 0x0000001F ; //pow(2,ceil((8430*1.0/2048)))-1;//ceil((8430*1.0/2048))-1;
	//buffermap bm = create_buffermap(db.tab_file[gp_file].buffer_map, db.tab_file[gp_file].size);
	//print_buffermap(bm);
	//printf("taille du buffermap : %d\n", bm.size);
	fprintf(stderr, "decimal = %lld\n", bm);
	printf("query = %s\n", data_query(query, gp_file, bm));
	//data_query(query, gp_file, bm);
	
	
    FILE *f = fopen("test", "wb");   
    fwrite(query, QUERY_SIZE, 1, f);    
    fclose(f);
    
    
	fprintf(stderr, "bm = %lld\n", bm);
	//int fd = open("file_query", O_RDWR|O_CREAT);
	//recompose("result2.jpg", query, gp_file, bm);
	//write(fd, query, 4112);
	
	
	//data_query(query, gp_file, bm);
	//char * written_file_name = "result2.jpg";
	//recompose(written_file_name, query, gp_file, bm);
	return 0;
}
#endif

